package com.lianxi.controller;

import com.lianxi.mapper.LoginMapper;
import com.lianxi.mapper.UserMapper;
import com.lianxi.pojo.BaseUser;
import com.lianxi.pojo.User;
import com.lianxi.service.LoginService;
import com.lianxi.utils.CommonResult;
import com.lianxi.utils.CurrentUtil;
import com.lianxi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@CrossOrigin
@RestController
@Api(tags = "登录注册接口")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    public CommonResult login(String username, String password) {
        return CommonResult.successNoData();
    }

    @PostMapping("/logout")
    public CommonResult logout() {
        return CommonResult.successNoData();
    }



    @ApiOperation("用户的注册")
    @PostMapping(value = "/register")
    public CommonResult reginUser(@RequestBody BaseUser baseUser) {
        User user = new User();
        user.setUsername(baseUser.getUsername());
        user.setPassword(passwordEncoder.encode(baseUser.getPassword()));
        user.setEmail(baseUser.getEmail());
        user.setNickname(baseUser.getNickname());
        user.setUserface(baseUser.getUserface());
        boolean result = loginService.reginUser(user);
        if (result) {
            return CommonResult.success(loginService.getUserByUsername(user.getUsername()));
        } else {
            return CommonResult.fail();
        }
    }

    @ApiOperation("用户名是否已经存在")
    @ApiImplicitParam(name = "username", value = "用户名", dataType = "string")
    @GetMapping(value = "/checkusername/{username}")
    public CommonResult checkUsername(@PathVariable("username") String username) {
        boolean result = loginService.checkUsername(username);
        return result ? CommonResult.success(result) : CommonResult.failHaveData(result);
    }

    @ApiOperation("修改用户密码")
    @PutMapping(value = "/updapwd")
    public CommonResult updaUserByUid(String password) {
        int i = loginService.updaPwdByUid(password);
        if (i < 0) {
            return CommonResult.error("修改密码失败！");
        } else {
            return CommonResult.successNoData();
        }
    }

    @ApiOperation("修改用户邮箱昵称")
    @PutMapping("/updauser")
    public CommonResult updaUserByUid(@RequestBody BaseUser user) {
        int i = loginService.updaUserByUid(user.getNickname(), user.getEmail());
        User userInfo = loginService.getUserByUsername(CurrentUtil.getcurrentUserName());
        if (i < 0) {
            return CommonResult.error("修改用户信息失败！");
        } else {
            return CommonResult.success(userInfo);
        }
    }



}



